# -*- coding: utf-8 -*-
# @Time    : 2021/10/24 17:53
# @Author  : super

import socket
import threading

def socket_client(service_client_socket, ip_port):
    while True:
        # 接收客户端发送的数据
        recv_data = service_client_socket.recv(1024)
        if recv_data:
            print(recv_data.decode("gbk"))
            # 对客户端发送数据
            service_client_socket.send("问题正在处理中....".encode("gbk"))
        else:
            print("客户端下线了")
            break
    # 关闭套件字
    service_client_socket.close()

if __name__ == '__main__':
    #创建套件字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #设置端口复用
    tcp_server_socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    tcp_server_socket.bind(("", 9091))
    #指定最大连接数
    tcp_server_socket.listen(128)
    while True:
        # 堵塞到客户开始链接
        service_client_socket, ip_port = tcp_server_socket.accept()
        print("客户端ip地址:", ip_port)
        sub_thread = threading.Thread(target = socket_client, args = (service_client_socket, ip_port))
        #设置主线程守护
        sub_thread.setDaemon(True)
        #启动主线程
        sub_thread.start()


    #关闭链接
    tcp_server_socket.close()